﻿@using Smartstore.Utilities
@using Smartstore.Web.Models.Customers

@model CustomerDownloadableProductsModel

@{
    Assets.AppendTitleParts(T("PageTitle.Account"));

    Layout = "_MyAccount";
    ViewBag.PageClassToken = "downloads";
    ViewBag.PageTitle = T("Account.DownloadableProducts").Value;
}

@if (Model.Items.Count == 0)
{
    <div class="alert alert-warning" role="status" aria-live="polite">
        @T("DownloadableProducts.NoItems")
    </div>
    return;
}

<span id="esd-order-details" class="sr-only">@T("Order.OrderDetails")</span>

<ul class="esd-list list-unstyled" aria-labelledby="myaccount-header">
    @foreach (var item in Model.Items)
    {
        <li class="esd-item mb-3" aria-label="@T("Aria.Label.ProductOfOrderPlacedOn", item.OrderId, item.CreatedOn.ToNativeString("d"), item.ProductName)">
            <div class="card shadow-sm">
                <div class="card-body">
                    <div class="card-title h4 mb-4" sm-language-attributes-for="item.ProductName" title="@item.ProductName">
                        @item.ProductName.Value.Truncate(50, "…")
                    </div>
                    <dl class="row mb-0" role="presentation">
                        <dt class="col-sm-3 fwn text-muted">@T("Order.Order#")</dt>
                        <dd class="col-sm-9">
                            <a id="esd-order-link@(item.Id)"
                               asp-action="Details" 
                               asp-controller="Order" 
                               asp-route-id="@item.OrderId" 
                               aria-labelledby="esd-order-details esd-order-link@(item.Id)"
                               rel="nofollow">@item.OrderId</a>
                        </dd>
                        <dt class="col-sm-3 fwn text-muted">@T("Common.CreatedOn")</dt>
                        <dd class="col-sm-9">
                            <time asp-for="@item.CreatedOn" sm-format="d"></time>
                        </dd>
                        <dt class="col-sm-3 fwn text-muted">@T("DownloadableProducts.Fields.Product")</dt>
                        <dd class="col-sm-9">
                            <a href="@item.ProductUrl" sm-language-attributes-for="item.ProductName">
                                @item.ProductName
                            </a>
                            <div sm-if="item.ProductAttributes.HasValue()">
                                @Html.Raw(item.ProductAttributes)
                            </div>
                        </dd>
                    </dl>
                </div>

                @if (item.IsDownloadAllowed || item.LicenseId > 0)
                {
                    <div class="card-footer d-flex p-2">
                        @if (item.IsDownloadAllowed)
                        {
                            <a class="btn btn-light btn-sm btn-flat" asp-action="GetDownload" asp-controller="Download" asp-route-id="@item.OrderItemGuid" rel="nofollow">
                                <i class="fa fa-download" aria-hidden="true"></i>
                                <span>@T("DownloadableProducts.Fields.Download")</span>
                                <span class="sr-only"> @item.ProductName</span>
                            </a>
                            <button sm-if="item.DownloadVersions.Count > 1" 
                               class="btn btn-light btn-sm btn-flat" 
                               data-toggle="collapse" 
                               data-target="#download-versions-@item.OrderId"
                               aria-controls="download-versions-@item.OrderId"
                               aria-expanded="false">
                                <i class="fa fa-info" aria-hidden="true"></i>
                                <span>@T("Common.Download.Versions")</span>
                                <span class="sr-only"> @item.ProductName</span>
                            </button>
                        }
                        else
                        {
                            <a class="btn btn-light btn-sm btn-flat disabled" href="javascript:;" rel="nofollow">
                                <span>@T("DownloadableProducts.Fields.Download.NA")</span>
                            </a>
                        }

                        @if (item.LicenseId > 0)
                        {
                            <a class="btn btn-light btn-sm btn-flat" asp-action="GetLicense" asp-controller="Download" asp-route-id="@item.OrderItemGuid" rel="nofollow">
                                <span>@T("DownloadableProducts.Fields.DownloadLicense")</span>
                                <span class="sr-only"> @item.ProductName</span>
                            </a>
                        }
                    </div>

                    <div sm-if="item.IsDownloadAllowed && item.DownloadVersions.Count > 1" 
                         id="download-versions-@item.OrderId"
                         class="collapse download-versions">
                        <table class="table mb-0">
                            <caption class="sr-only">@T("Common.Download.Versions") @item.ProductName</caption>
                            <thead>
                                <tr>
                                    <th scope="col"><span class="sr-only">@T("Common.ExpandCollapse")</span></th>
                                    <th scope="col">@T("Common.Download.Version")</th>
                                    <th scope="col">@T("Common.File")</th>
                                </tr>
                            </thead>
                            <tbody id="download-versions">
                                @foreach (var download in item.DownloadVersions)
                                {
                                    var id = $"download-version-{CommonHelper.GenerateRandomInteger()}";
                                    <tr role="button"
                                        tabindex="0"
                                        data-toggle="collapse" 
                                        data-target="#@id" 
                                        aria-controls="@id"
                                        aria-expanded="false"
                                        class="download-version">
                                        <td>
                                            <i class="fa fa-fw fa-angle-right" aria-hidden="true"></i>
                                        </td>
                                        <td>
                                            @download.FileVersion
                                        </td>
                                        <td>
                                            <a asp-action="GetDownload" 
                                               asp-controller="Download" 
                                               asp-route-id="@item.OrderItemGuid"
                                               asp-route-fileVersion="@download.FileVersion" 
                                               class="download-link">
                                                <span>@download.FileName</span>
                                                <span class="sr-only"> @download.FileVersion</span>
                                            </a>
                                        </td>
                                    </tr>
                                    <tr id="@id" class="download-changelog collapse bg-light">
                                        <td colspan="3">
                                            <div>
                                                @if (download.Changelog.HasValue())
                                                {
                                                    @Html.Raw(download.Changelog)
                                                }
                                                else
                                                {
                                                    @T("Customer.Downloads.NoChangelogAvailable")
                                                }
                                            </div>
                                        </td>
                                    </tr>
                                }
                            </tbody>
                        </table>
                    </div>
                }
            </div>
        </li>
    }
</ul>

<script sm-target-zone="scripts" data-origin="downloadable-products">
    $(function () {
        $('.download-versions .download-changelog').on('show.bs.collapse hide.bs.collapse', function (e) {
            var icon = $(e.target).prev().find("i");
            icon.toggleClass("fa-angle-down", e.type == "show");
        });

        $('.download-versions .download-link').on("click", function (e) {
            e.stopPropagation();
        });
    });
</script>